package com.tao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tao.pojo.User;
import com.tao.pojo.vo.LoginUser;
import com.tao.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.ArrayList;
import java.util.Arrays;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private UserService userService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        /** 查询数据库认证 */
        LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
        lqw.eq(User::getUsername,username);
        User user = userService.getOne(lqw);
        if (ObjectUtils.isEmpty(user)){
            throw new RuntimeException("用户名不存在");
        }
        /** 数据库授权 */
        ArrayList<String> list = new ArrayList<>(Arrays.asList("admin", "user", "role"));
        return new LoginUser(user,list);
    }
}
